﻿<div style="color:Black;background-color:White;"><pre>
<span style="color:Green;">################################################################################ </span>
<span style="color:Green;"># Set-ClipboardScript.ps1 </span>
<span style="color:Green;"># </span>
<span style="color:Green;"># The script entire contents of the currently selected editor window to system </span>
<span style="color:Green;"># clipboard. The copied data can be pasted into any application that supports </span>
<span style="color:Green;"># pasting in UnicodeText, RTF or HTML format. Text pasted in RTF or HTML </span>
<span style="color:Green;"># format will be colorized. </span>
<span style="color:Green;"># </span>
<span style="color:Green;"># See also: </span>
<span style="color:Green;"># http://blogs.msdn.com/powershell/archive/2009/01/13/ </span>
<span style="color:Green;"># how-to-copy-colorized-script-from-powershell-ise.aspx </span>
<span style="color:Green;"># http://www.leeholmes.com/blog/SyntaxHighlightingInPowerShell.aspx </span>
<span style="color:Green;"># http://www.leeholmes.com/blog/RealtimeSyntaxHighlightingInYourPowerShellConsole.aspx </span>
<span style="color:Green;"># </span>
<span style="color:Green;">################################################################################ </span>

[<span style="color:PowderBlue;">CmdletBinding</span>()] 
<span style="color:Blue;">param</span>(<span style="color:OrangeRed;">$path</span>) 

<span style="color:Blue;">function</span> Get<span style="color:Gray;">-</span>ScriptName 
{ 
    <span style="color:OrangeRed;">$myInvocation</span>.ScriptName 
} 

<span style="color:Blue;">if</span>(<span style="color:OrangeRed;">$path</span> <span style="color:Gray;">-and</span> (<span style="color:Gray;">[</span><span style="color:Teal;">Threading.Thread</span><span style="color:Gray;">]</span><span style="color:Gray;">::</span>CurrentThread.ApartmentState <span style="color:Gray;">-ne</span> <span style="color:#A31515;">&quot;STA&quot;</span>)) 
{ 
    PowerShell <span style="color:Gray;">-</span>NoProfile <span style="color:Gray;">-</span>STA <span style="color:Gray;">-</span>File (Get<span style="color:Gray;">-</span>ScriptName) <span style="color:OrangeRed;">$path</span> 
    <span style="color:Blue;">return</span> 
} 

<span style="color:OrangeRed;">$tokenColours</span> <span style="color:Gray;">=</span> @{ 
    <span style="color:#A31515;">'Attribute'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FFADD8E6'</span> 
    <span style="color:#A31515;">'Command'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF0000FF'</span> 
    <span style="color:#A31515;">'CommandArgument'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF8A2BE2'</span> 
    <span style="color:#A31515;">'CommandParameter'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000080'</span> 
    <span style="color:#A31515;">'Comment'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF006400'</span> 
    <span style="color:#A31515;">'GroupEnd'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'GroupStart'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'Keyword'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF00008B'</span> 
    <span style="color:#A31515;">'LineContinuation'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'LoopLabel'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF00008B'</span> 
    <span style="color:#A31515;">'Member'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'NewLine'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'Number'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF800080'</span> 
    <span style="color:#A31515;">'Operator'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FFA9A9A9'</span> 
    <span style="color:#A31515;">'Position'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'StatementSeparator'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'String'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF8B0000'</span> 
    <span style="color:#A31515;">'Type'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF008080'</span> 
    <span style="color:#A31515;">'Unknown'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FF000000'</span> 
    <span style="color:#A31515;">'Variable'</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'#FFFF4500'</span> 
} 

<span style="color:Blue;">if</span>(<span style="color:OrangeRed;">$psise</span>) 
{ 
    <span style="color:OrangeRed;">$tokenColours</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$psise</span>.Options.TokenColors 
} 

Add<span style="color:Gray;">-</span>Type <span style="color:Gray;">-</span>Assembly System.Web 
Add<span style="color:Gray;">-</span>Type <span style="color:Gray;">-</span>Assembly PresentationCore 

<span style="color:Green;"># Create RTF block from text using named console colors. </span>
<span style="color:Blue;">function</span> Append<span style="color:Gray;">-</span>RtfBlock (<span style="color:OrangeRed;">$block</span>, <span style="color:OrangeRed;">$tokenColor</span>) 
{ 
    <span style="color:OrangeRed;">$colorIndex</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfColorMap</span>.<span style="color:OrangeRed;">$tokenColor</span> 
    <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$block</span>.Replace(<span style="color:#A31515;">'\','</span>\\').Replace(<span style="color:#A31515;">&quot;`r`n&quot;</span>,<span style="color:#A31515;">&quot;\cf1\par`r`n&quot;</span>) 
    <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$block</span>.Replace(<span style="color:#A31515;">&quot;`t&quot;</span>,<span style="color:#A31515;">'\tab'</span>).Replace(<span style="color:#A31515;">'{'</span>,<span style="color:#A31515;">'\{'</span>).Replace(<span style="color:#A31515;">'}'</span>,<span style="color:#A31515;">'\}'</span>) 
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">&quot;\cf$colorIndex $block&quot;</span>) 
} 

<span style="color:Green;"># Generate an HTML span and append it to HTML string builder </span>
<span style="color:OrangeRed;">$currentLine</span> <span style="color:Gray;">=</span> 1 
<span style="color:Blue;">function</span> Append<span style="color:Gray;">-</span>HtmlSpan (<span style="color:OrangeRed;">$block</span>, <span style="color:OrangeRed;">$tokenColor</span>) 
{ 
    <span style="color:Blue;">if</span> ((<span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">-eq</span> <span style="color:#A31515;">'NewLine'</span>) <span style="color:Gray;">-or</span> (<span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">-eq</span> <span style="color:#A31515;">'LineContinuation'</span>)) 
    { 
        <span style="color:Blue;">if</span>(<span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">-eq</span> <span style="color:#A31515;">'LineContinuation'</span>) 
        { 
            <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$codeBuilder</span>.Append(<span style="color:#A31515;">'`'</span>) 
        } 
        
        <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$codeBuilder</span>.Append(<span style="color:#A31515;">&quot;&lt;br /&gt;`r`n&quot;</span>) 
        <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$lineBuilder</span>.Append(<span style="color:#A31515;">&quot;{0:000}&lt;BR /&gt;&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$currentLine</span>) 
        <span style="color:OrangeRed;">$SCRIPT:currentLine</span><span style="color:Gray;">++</span> 
    } 
    <span style="color:Blue;">else</span> 
    { 
        <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:Gray;">[</span><span style="color:Teal;">System.Web.HttpUtility</span><span style="color:Gray;">]</span><span style="color:Gray;">::</span>HtmlEncode(<span style="color:OrangeRed;">$block</span>) 
        <span style="color:Blue;">if</span> (<span style="color:Gray;">-not</span> <span style="color:OrangeRed;">$block</span>.Trim()) 
        { 
            <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$block</span>.Replace(<span style="color:#A31515;">' '</span>, <span style="color:#A31515;">'&amp;nbsp;'</span>) 
        } 

        <span style="color:OrangeRed;">$htmlColor</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$tokenColours</span><span style="color:Gray;">[</span><span style="color:Teal;">$tokenColor</span><span style="color:Gray;">]</span>.ToString().Replace(<span style="color:#A31515;">'#FF'</span>, <span style="color:#A31515;">'#'</span>) 

        <span style="color:Blue;">if</span>(<span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">-eq</span> <span style="color:#A31515;">'String'</span>) 
        { 
            <span style="color:OrangeRed;">$lines</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">-split</span> <span style="color:#A31515;">&quot;`r`n&quot;</span> 
            <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">&quot;&quot;</span> 

            <span style="color:OrangeRed;">$multipleLines</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$false</span> 
            <span style="color:Blue;">foreach</span>(<span style="color:OrangeRed;">$line</span> <span style="color:Blue;">in</span> <span style="color:OrangeRed;">$lines</span>) 
            { 
                <span style="color:Blue;">if</span>(<span style="color:OrangeRed;">$multipleLines</span>) 
                { 
                    <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">+=</span> <span style="color:#A31515;">&quot;&lt;BR /&gt;`r`n&quot;</span> 
                    
                    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$lineBuilder</span>.Append(<span style="color:#A31515;">&quot;{0:000}&lt;BR /&gt;&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$currentLine</span>) 
                    <span style="color:OrangeRed;">$SCRIPT:currentLine</span><span style="color:Gray;">++</span> 
                } 

                <span style="color:OrangeRed;">$newText</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$line</span>.TrimStart() 
                <span style="color:OrangeRed;">$newText</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">&quot;&amp;nbsp;&quot;</span> <span style="color:Gray;">*</span> (<span style="color:OrangeRed;">$line</span>.Length <span style="color:Gray;">-</span> <span style="color:OrangeRed;">$newText</span>.Length) <span style="color:Gray;">+</span>  
                    <span style="color:OrangeRed;">$newText</span> 
                <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">+=</span> <span style="color:OrangeRed;">$newText</span> 
                <span style="color:OrangeRed;">$multipleLines</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$true</span> 
            } 
        } 
    
        <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$codeBuilder</span>.Append( 
            <span style="color:#A31515;">&quot;&lt;span style='color:$htmlColor'&gt;$block&lt;/span&gt;&quot;</span>) 
    } 
} 

<span style="color:Blue;">function</span> GetHtmlClipboardFormat(<span style="color:OrangeRed;">$html</span>) 
{ 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">@&quot; 
Version:1.0 
StartHTML:0000000000 
EndHTML:0000000000 
StartFragment:0000000000 
EndFragment:0000000000 
StartSelection:0000000000 
EndSelection:0000000000 
SourceURL:file:///about:blank 
&lt;!DOCTYPE HTML PUBLIC `&quot;-//W3C//DTD HTML 4.0 Transitional//EN`&quot;&gt; 
&lt;HTML&gt; 
&lt;HEAD&gt; 
&lt;TITLE&gt;HTML Clipboard&lt;/TITLE&gt; 
&lt;/HEAD&gt; 
&lt;BODY&gt; 
&lt;!--StartFragment--&gt; 
&lt;DIV style='font-family:Consolas,Lucida Console; font-size:10pt; 
    width:750; border:1px solid black; overflow:auto; padding:5px'&gt; 
&lt;TABLE BORDER='0' cellpadding='5' cellspacing='0'&gt; 
&lt;TBODY&gt; 
&lt;TR&gt; 
    &lt;TD VALIGN='Top'&gt; 
&lt;DIV style='font-family:Consolas,Lucida Console; font-size:10pt; 
    padding:5px; background:#cecece'&gt; 
__LINES__ 
&lt;/DIV&gt; 
    &lt;/TD&gt; 
    &lt;TD VALIGN='Top' NOWRAP='NOWRAP'&gt; 
&lt;DIV style='font-family:Consolas,Lucida Console; font-size:10pt; 
    padding:5px; background:#fcfcfc'&gt; 
__HTML__ 
&lt;/DIV&gt; 
    &lt;/TD&gt; 
&lt;/TR&gt; 
&lt;/TBODY&gt; 
&lt;/TABLE&gt; 
&lt;/DIV&gt; 
&lt;!--EndFragment--&gt; 
&lt;/BODY&gt; 
&lt;/HTML&gt; 
&quot;@</span> 

    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span>.Replace(<span style="color:#A31515;">&quot;__LINES__&quot;</span>, <span style="color:OrangeRed;">$lineBuilder</span>.ToString()) 
    <span style="color:OrangeRed;">$startFragment</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span>.IndexOf(<span style="color:#A31515;">&quot;&lt;!--StartFragment--&gt;&quot;</span>) <span style="color:Gray;">+</span> 
        <span style="color:#A31515;">&quot;&lt;!--StartFragment--&gt;&quot;</span>.Length <span style="color:Gray;">+</span> 2 
    <span style="color:OrangeRed;">$endFragment</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span>.IndexOf(<span style="color:#A31515;">&quot;&lt;!--EndFragment--&gt;&quot;</span>) <span style="color:Gray;">+</span> 
        <span style="color:OrangeRed;">$html</span>.Length <span style="color:Gray;">-</span> <span style="color:#A31515;">&quot;__HTML__&quot;</span>.Length 
    <span style="color:OrangeRed;">$startHtml</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span>.IndexOf(<span style="color:#A31515;">&quot;&lt;!DOCTYPE&quot;</span>) 
    <span style="color:OrangeRed;">$endHtml</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span>.Length <span style="color:Gray;">+</span> <span style="color:OrangeRed;">$html</span>.Length <span style="color:Gray;">-</span> <span style="color:#A31515;">&quot;__HTML__&quot;</span>.Length 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">-replace</span> <span style="color:#A31515;">&quot;StartHTML:0000000000&quot;</span>, 
        (<span style="color:#A31515;">&quot;StartHTML:{0:0000000000}&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$startHtml</span>) 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">-replace</span> <span style="color:#A31515;">&quot;EndHTML:0000000000&quot;</span>, 
        (<span style="color:#A31515;">&quot;EndHTML:{0:0000000000}&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$endHtml</span>) 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">-replace</span> <span style="color:#A31515;">&quot;StartFragment:0000000000&quot;</span>, 
        (<span style="color:#A31515;">&quot;StartFragment:{0:0000000000}&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$startFragment</span>) 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">-replace</span> <span style="color:#A31515;">&quot;EndFragment:0000000000&quot;</span>, 
        (<span style="color:#A31515;">&quot;EndFragment:{0:0000000000}&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$endFragment</span>) 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">-replace</span> <span style="color:#A31515;">&quot;StartSelection:0000000000&quot;</span>, 
        (<span style="color:#A31515;">&quot;StartSelection:{0:0000000000}&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$startFragment</span>) 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">-replace</span> <span style="color:#A31515;">&quot;EndSelection:0000000000&quot;</span>, 
        (<span style="color:#A31515;">&quot;EndSelection:{0:0000000000}&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$endFragment</span>) 
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$header</span>.Replace(<span style="color:#A31515;">&quot;__HTML__&quot;</span>, <span style="color:OrangeRed;">$html</span>) 
    
    Write<span style="color:Gray;">-</span>Verbose <span style="color:OrangeRed;">$header</span> 
    <span style="color:OrangeRed;">$header</span> 
} 

<span style="color:Blue;">function</span> Main 
{ 
    <span style="color:OrangeRed;">$text</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$null</span> 
    
    <span style="color:Blue;">if</span>(<span style="color:OrangeRed;">$path</span>) 
    { 
        <span style="color:OrangeRed;">$text</span> <span style="color:Gray;">=</span> (Get<span style="color:Gray;">-</span>Content <span style="color:OrangeRed;">$path</span>) <span style="color:Gray;">-join</span> <span style="color:#A31515;">&quot;`r`n&quot;</span> 
    } 
    <span style="color:Blue;">else</span> 
    { 
        <span style="color:Blue;">if</span> (<span style="color:Gray;">-not</span> <span style="color:OrangeRed;">$psise</span>.CurrentOpenedFile) 
        { 
            Write<span style="color:Gray;">-</span>Error <span style="color:#A31515;">'No script is available for copying.'</span> 
            <span style="color:Blue;">return</span> 
        } 
        
        <span style="color:OrangeRed;">$text</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$psise</span>.CurrentOpenedFile.Editor.Text 
    } 

    <span style="color:Blue;">trap</span> { <span style="color:Blue;">break</span> } 

    <span style="color:Green;"># Do syntax parsing. </span>
    <span style="color:OrangeRed;">$errors</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$null</span> 
    <span style="color:OrangeRed;">$tokens</span> <span style="color:Gray;">=</span> <span style="color:Gray;">[</span><span style="color:Teal;">system.management.automation.psparser</span><span style="color:Gray;">]</span><span style="color:Gray;">::</span>Tokenize(<span style="color:OrangeRed;">$Text</span>, 
        <span style="color:Gray;">[</span><span style="color:Teal;">ref</span><span style="color:Gray;">]</span> <span style="color:OrangeRed;">$errors</span>) 

    <span style="color:Green;"># Initialize HTML builder. </span>
    <span style="color:OrangeRed;">$codeBuilder</span> <span style="color:Gray;">=</span> new<span style="color:Gray;">-</span>object system.text.stringbuilder 
    <span style="color:OrangeRed;">$lineBuilder</span> <span style="color:Gray;">=</span> new<span style="color:Gray;">-</span>object system.text.stringbuilder 
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$lineBuilder</span>.Append(<span style="color:#A31515;">&quot;{0:000}&lt;BR /&gt;&quot;</span> <span style="color:Gray;">-</span>f <span style="color:OrangeRed;">$currentLine</span>) 
    <span style="color:OrangeRed;">$SCRIPT:currentLine</span><span style="color:Gray;">++</span> 
   

    <span style="color:Green;"># Initialize RTF builder. </span>
    <span style="color:OrangeRed;">$rtfBuilder</span> <span style="color:Gray;">=</span> new<span style="color:Gray;">-</span>object system.text.stringbuilder 
    
    <span style="color:Green;"># Append RTF header </span>
    <span style="color:OrangeRed;">$header</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">&quot;{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl&quot;</span> <span style="color:Gray;">+</span> 
        <span style="color:#A31515;">&quot;{\f0\fnil\fcharset0 $fontName;}}&quot;</span> 
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:OrangeRed;">$header</span>) 
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">&quot;`r`n&quot;</span>) 

    <span style="color:Green;"># Append RTF color table which will contain all Powershell console colors. </span>
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">&quot;{\colortbl ;&quot;</span>) 
    
    <span style="color:Green;"># Generate RTF color definitions for each token type. </span>
    <span style="color:OrangeRed;">$rtfColorIndex</span> <span style="color:Gray;">=</span> 1 
    <span style="color:OrangeRed;">$rtfColors</span> <span style="color:Gray;">=</span> @{} 
    <span style="color:OrangeRed;">$rtfColorMap</span> <span style="color:Gray;">=</span> @{} 
    
    <span style="color:Gray;">[</span><span style="color:Teal;">Enum</span><span style="color:Gray;">]</span><span style="color:Gray;">::</span>GetNames(<span style="color:Gray;">[</span><span style="color:Teal;">System.Management.Automation.PSTokenType</span><span style="color:Gray;">]</span>) | <span style="color:Gray;">%</span> { 
        <span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$tokenColours</span><span style="color:Gray;">[</span><span style="color:Teal;">$_</span><span style="color:Gray;">]</span>; 
        <span style="color:OrangeRed;">$rtfColor</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">&quot;\red$($tokenColor.R)\green$($tokenColor.G)\blue&quot;</span> <span style="color:Gray;">+</span> 
            <span style="color:#A31515;">&quot;$($tokenColor.B);&quot;</span> 
        <span style="color:Blue;">if</span> (<span style="color:OrangeRed;">$rtfColors</span>.Keys <span style="color:Gray;">-notcontains</span> <span style="color:OrangeRed;">$rtfColor</span>) 
        { 
            <span style="color:OrangeRed;">$rtfColors</span>.<span style="color:OrangeRed;">$rtfColor</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfColorIndex</span> 
            <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:OrangeRed;">$rtfColor</span>) 
            <span style="color:OrangeRed;">$rtfColorMap</span>.<span style="color:OrangeRed;">$_</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfColorIndex</span> 
            <span style="color:OrangeRed;">$rtfColorIndex</span> <span style="color:Gray;">++</span> 
        } 
        <span style="color:Blue;">else</span> 
        { 
            <span style="color:OrangeRed;">$rtfColorMap</span>.<span style="color:OrangeRed;">$_</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfColors</span>.<span style="color:OrangeRed;">$rtfColor</span> 
        } 
    } 
    
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">'}'</span>) 
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">&quot;`r`n&quot;</span>) 
    
    <span style="color:Green;"># Append RTF document settings. </span>
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">'\viewkind4\uc1\pard\f0\fs20 '</span>) 
    
    <span style="color:Green;"># Iterate over the tokens and set the colors appropriately. </span>
    <span style="color:OrangeRed;">$position</span> <span style="color:Gray;">=</span> 0 
    <span style="color:Blue;">foreach</span> (<span style="color:OrangeRed;">$token</span> <span style="color:Blue;">in</span> <span style="color:OrangeRed;">$tokens</span>) 
    { 
        <span style="color:Blue;">if</span> (<span style="color:OrangeRed;">$position</span> <span style="color:Gray;">-lt</span> <span style="color:OrangeRed;">$token</span>.Start) 
        { 
            <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$text</span>.Substring(<span style="color:OrangeRed;">$position</span>, (<span style="color:OrangeRed;">$token</span>.Start <span style="color:Gray;">-</span> <span style="color:OrangeRed;">$position</span>)) 
            <span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">'Unknown'</span> 
            Append<span style="color:Gray;">-</span>RtfBlock <span style="color:OrangeRed;">$block</span> <span style="color:OrangeRed;">$tokenColor</span> 
            Append<span style="color:Gray;">-</span>HtmlSpan <span style="color:OrangeRed;">$block</span> <span style="color:OrangeRed;">$tokenColor</span> 
        } 
        
        <span style="color:OrangeRed;">$block</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$text</span>.Substring(<span style="color:OrangeRed;">$token</span>.Start, <span style="color:OrangeRed;">$token</span>.Length) 
        <span style="color:OrangeRed;">$tokenColor</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$token</span>.Type.ToString() 
        Append<span style="color:Gray;">-</span>RtfBlock <span style="color:OrangeRed;">$block</span> <span style="color:OrangeRed;">$tokenColor</span> 
        Append<span style="color:Gray;">-</span>HtmlSpan <span style="color:OrangeRed;">$block</span> <span style="color:OrangeRed;">$tokenColor</span> 
        
        <span style="color:OrangeRed;">$position</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$token</span>.Start <span style="color:Gray;">+</span> <span style="color:OrangeRed;">$token</span>.Length 
    } 

    <span style="color:Green;"># Append RTF ending brace. </span>
    <span style="color:OrangeRed;">$null</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.Append(<span style="color:#A31515;">'}'</span>) 
    
    <span style="color:Green;"># Copy console screen buffer contents to clipboard in three formats - </span>
    <span style="color:Green;"># text, HTML and RTF. </span>
    <span style="color:OrangeRed;">$dataObject</span> <span style="color:Gray;">=</span> New<span style="color:Gray;">-</span>Object Windows.DataObject 
    <span style="color:OrangeRed;">$dataObject</span>.SetText(<span style="color:Gray;">[</span><span style="color:Teal;">string</span><span style="color:Gray;">]</span><span style="color:OrangeRed;">$text</span>, <span style="color:Gray;">[</span><span style="color:Teal;">Windows.TextDataFormat</span><span style="color:Gray;">]</span><span style="color:#A31515;">&quot;UnicodeText&quot;</span>) 
    <span style="color:OrangeRed;">$rtf</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$rtfBuilder</span>.ToString() 
    <span style="color:OrangeRed;">$dataObject</span>.SetText(<span style="color:Gray;">[</span><span style="color:Teal;">string</span><span style="color:Gray;">]</span><span style="color:OrangeRed;">$rtf</span>, <span style="color:Gray;">[</span><span style="color:Teal;">Windows.TextDataFormat</span><span style="color:Gray;">]</span><span style="color:#A31515;">&quot;Rtf&quot;</span>) 
    <span style="color:OrangeRed;">$code</span> <span style="color:Gray;">=</span> <span style="color:OrangeRed;">$codeBuilder</span>.ToString() 
    <span style="color:OrangeRed;">$html</span> <span style="color:Gray;">=</span> GetHtmlClipboardFormat(<span style="color:OrangeRed;">$code</span>) 
    
    <span style="color:OrangeRed;">$dataObject</span>.SetText(<span style="color:Gray;">[</span><span style="color:Teal;">string</span><span style="color:Gray;">]</span><span style="color:OrangeRed;">$html</span>, <span style="color:Gray;">[</span><span style="color:Teal;">Windows.TextDataFormat</span><span style="color:Gray;">]</span><span style="color:#A31515;">&quot;Html&quot;</span>) 

    <span style="color:Gray;">[</span><span style="color:Teal;">Windows.Clipboard</span><span style="color:Gray;">]</span><span style="color:Gray;">::</span>SetDataObject(<span style="color:OrangeRed;">$dataObject</span>, <span style="color:OrangeRed;">$true</span>) 
} 

. Main
</pre></div>